<template>
	<div class="category-list">
		<list-title :data="header" @changeVisible="changeVisible"></list-title>
		<list-table :list="categoryList" :tableLoading="tableLoading" :footer="false" :sort="true" @setSort="setSort">
			<template v-slot:data>
				<el-table-column prop="cat_name" label="分类名称"></el-table-column>
				<el-table-column prop="icon" label="Icon">
					<template slot-scope="scope">
						<span :class="'icon iconfont icon-' + scope.row.icon"></span>
					</template>
				</el-table-column>
				<el-table-column label="显示状态">
					<template slot-scope="scope">
						<el-switch v-model="scope.row.display" @change="changeDisplay(scope.$index, scope.row)"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="200">
					<template slot-scope="scope">
						<el-button type='text' size="mini" @click="handleEdit(scope.$index, scope.row)" v-if="auth.editor">编辑</el-button>
						<el-popover
						placement="top"
						width="160"
						v-model="delTip[scope.$index]">
							<p class="del-tip"><span class="el-icon-question"></span>确定删除吗？</p>
							<div style="text-align: right; margin: 0">
								<el-button size="mini" type="text" @click="delTip.splice(scope.$index, 1, false)">取消</el-button>
								<el-button type="primary" size="mini" @click="handleDelete(scope.$index, scope.row)">确定</el-button>
							</div>
							<el-button type="text" slot="reference" :disabled='submitLock[scope.$index]' v-if="auth.delete">
								删除 <span class="el-icon-loading" v-if='submitLock[scope.$index]'></span>
							</el-button>
						</el-popover>
					</template>
				</el-table-column>
			</template>
		</list-table>
		<!-- 添加分类 开始 -->
		<el-dialog :title="dialogTitle" :visible.sync="header.dialogs[0].visible" :close-on-click-modal="false" width="460px">
			<el-form :model="categoryForm" :rules="rules" ref="categoryForm" label-width="100px">
				<el-form-item label="应用名称" prop="data.cat_name" style="width: 378px;">
					<el-input v-model="categoryForm.data.cat_name" placeholder="应用名称"></el-input>
				</el-form-item>
				<el-form-item label="Icon图标" prop="data.icon" style="width: 378px;">
					<el-input v-model="categoryForm.data.icon" placeholder="Icon"></el-input>
				</el-form-item>
				<el-form-item label="是否显示" prop="data.display">
					<el-switch v-model="categoryForm.data.display"></el-switch>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="header.dialogs[0].visible = false">取 消</el-button>
				<el-button type="primary" @click="submitForm('categoryForm')" :disabled='categoryForm.submitLock'>
					确 定 <span class="el-icon-loading" v-if='categoryForm.submitLock'></span>
				</el-button>
			</div>
		</el-dialog>
		<!-- 添加分类 结束 -->
	</div>
</template>

<script>
import ListTitle from "pc/common/Title"
import ListTable from "pc/common/ListTable"
import {changeToBool, getSortData, judgeAuth} from "static/js/common.js"
import {domain} from '@/api';
import {mapGetters} from 'vuex'

export default {
	inject: ['reload'],
	data() {
		return {
			/*头部数据*/
			header: {
				title: '应用分类列表',
				reload: true,
				dialogs: [
					{
						title: '添加分类',
						type: 'primary',
						visible: false,
						auth: false,
					}
				]
			},
			auth: {
				editor: false,//编辑
				delete: false,//删除
				changeState: false,//显示状态
				sort: false,//排序
			},
			dialogTitle: '添加分类',//dialog标题
			/*列表数据*/
			categoryList: [
				// {
				// 	id: 1,
				// 	cat_name: '我的面板',
				// 	icon: 'panel',
				// 	display: 2,
				// },
			],
			tableLoading: true,
			delTip: [],//刪除提示
			submitLock: [],
			/*添加表单数据*/
			categoryForm: {
				submitLock: false,
				data: {
					id: 0,
					cat_name: '',
					icon: '',
					display: false,
				}
			},
			rules: {
				data: {
					cat_name: [
						{ required: true, message: '请输入应用名称', trigger: 'blur' },
						{ min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
					]
				}
			}
		};
	},
	components: {
		ListTitle,
		ListTable
	},
	created: function() {
		/*获取分类数据*/
		this.getCategoryList()
		/*权限判断显示*/
		this.header.dialogs[0].auth = (judgeAuth('addAppCategory', this.allMenuList) == undefined)?false:true
		this.auth.editor = (judgeAuth('modifyAppCategory', this.allMenuList) == undefined)?false:true
		this.auth.delete = (judgeAuth('deleteAppCategory', this.allMenuList) == undefined)?false:true
		this.auth.changeState = (judgeAuth('setAppCategoryDisplay', this.allMenuList) == undefined)?false:true
		this.auth.sort = (judgeAuth('appCategorySort', this.allMenuList) == undefined)?false:true
	},
	methods: {
		/*获取分类数据*/
		getCategoryList: function(){
			// this.$baseURL('http://super.ac.comdd')
			this.$baseURL(domain.ac)
			if(this.$api.appCategoryList != undefined){
				this.$get(this.$api.appCategoryList).then(res => {
					// console.log(res);
					// if(res.data.code == 10007){//后台提示需要登录
					// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
					// 	this.reload();
					// }
					if(res.data.code === 0){
						if(res.data.data != undefined){
							this.categoryList = []
							Object.keys(res.data.data).forEach((k) => {
								this.categoryList.push ({
									id: res.data.data[k].app_cat_id,
									cat_name: res.data.data[k].cat_name,
									icon: res.data.data[k].icon,
									display: changeToBool(res.data.data[k].display)
								});
							})
						}
						this.tableLoading = false
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		},
		/*编辑菜单 打开编辑窗口，获取数据*/
		handleEdit(index, row) {
			// console.log(row.id);
			this.categoryForm.submitLock = true;
			/*取出需要修改的数据*/
			this.dialogTitle = '编辑分类'
			this.header.dialogs[0].visible = true//显示修改的窗口
			this.$nextTick(() => {
				this.$refs.categoryForm.resetFields()//清空表单
			})
			this.$baseURL(domain.ac)
			if(this.$api.modifyAppCategory != undefined){
				this.$get(this.$api.modifyAppCategory, {
					'id': row.id
				}).then(res => {
					// if(res.data.code == 10007){//后台提示需要登录
					// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
					// 	this.reload();
					// }
					if(res.data.code == 0){
						// console.log(res.data.data)
						this.categoryForm.data.id = res.data.data.app_cat_id
						this.categoryForm.data.cat_name = res.data.data.cat_name
						this.categoryForm.data.icon = res.data.data.icon
						this.categoryForm.data.display = changeToBool(res.data.data.display)
					}else{
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'error'
						});
					}
					this.categoryForm.submitLock = false;
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		},
		/*删除分类*/
		handleDelete(index, row) {
			if(!this.submitLock[index]){
				this.submitLock[index] = true//锁住当前的 删除按钮
				this.delTip.splice(index, 1, false)//提示消失
				/*请求后台删除*/
				this.$baseURL(domain.ac)
				if(this.$api.deleteAppCategory != undefined){
					this.$get(this.$api.deleteAppCategory, {
						'id': row.id
					}).then(res => {
						// if(res.data.code == 10007){//后台提示需要登录
						// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
						// 	this.reload();
						// }
						if(res.data.code == 0){
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'success'
							});
							this.categoryList.splice(index, 1);//删除当前行
						}else{
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'error'
							});
						}
						this.submitLock[index] = false//释放当前的 删除按钮
					})
					.catch(err => {})
					.finally(() => {}) 
				}
			}
		},
		/*dialog切换 方便子组件调用*/
		changeVisible: function(index){
			/*因为子组件调用的都是添加的行为，所以需要把id置为0(方便判断是添加还是修改)，title改为'添加分类'*/
			this.dialogTitle = '添加分类'
			this.categoryForm.data.id = 0

			this.header.dialogs[index].visible = !this.header.dialogs[index].visible
		},
		/*提交添加、修改表单*/
		submitForm(formName) {
			if(!this.categoryForm.submitLock){
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.categoryForm.submitLock = true;
						/*添加、修改分类*/
						let apiUrl = this.$api.addAppCategory//默认是添加
						if(this.categoryForm.data.id > 0){//修改
							apiUrl = this.$api.modifyAppCategory
						}
						this.$baseURL(domain.ac)
						if(apiUrl != undefined){
							this.$post(apiUrl, {
								AppCategory: this.categoryForm.data
							}).then(res => {
								// console.log(res);
								/*成功*/
								if(res.data.code == 0){
									this.getCategoryList()
									this.$message({
										showClose: true,
										message: res.data.info,
										type: 'success'
									});

									if(this.categoryForm.data.id > 0){
										this.reload();
									}else{
										this.$refs[formName].resetFields();
									}
								}else{
									this.$message({
										showClose: true,
										message: res.data.info,
										type: 'error'
									});
								}
								this.categoryForm.submitLock = false;
							});
						}
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			}
		},
		/*列表中直接修改 是否显示*/
		changeDisplay(index, row){
			// console.log(row.id)
			// console.log(row.display)
			/*请求后台*/
			this.$baseURL(domain.ac)
			if(this.$api.setAppCategoryDisplay != undefined){
				this.$post(this.$api.setAppCategoryDisplay, {
					'AppCategory': {
						'id': row.id,
						'display': row.display
					}
				}).then(res => {
					// if(res.data.code == 10007){//后台提示需要登录
					// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
					// 	this.reload();
					// }
					if(res.data.code == 0){
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'success'
						});
					}else{
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'error'
						});
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		},
		/*排序*/
		setSort(newIndex, oldIndex){
			// console.log(newIndex, oldIndex)
			this.categoryList = JSON.parse(JSON.stringify(this.categoryList))
			let oldVal = this.categoryList.splice(oldIndex, 1)[0]
			this.categoryList.splice(newIndex, 0, oldVal)
			// console.log(this.categoryList)
			let sort = getSortData(this.categoryList)
			// console.log(sort)

			this.$baseURL(domain.ac)
			if(this.$api.appCategorySort != undefined){
				this.$post(this.$api.appCategorySort, {
					'sort': sort
				}).then(res => {
					// if(res.data.code == 10007){//后台提示需要登录
					// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
					// 	this.reload();
					// }
					if(res.data.code == 0){
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'success'
						});
					}else{
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'error'
						});
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		}
	},
	computed: {
		...mapGetters([
			'allMenuList'
		])
	}
};
</script>

<style lang="stylus" scoped>
</style>